//
//  HACStockTimeLine.m
//  Pods
//
//  Created by Hotacool on 2017/9/5.
//
//

#import "HACStockTimeLine.h"
#import "HACTimeLineView.h"
#import "HACTecView.h"
#import "HACTimeLineEntry.h"
#import "HACStockTimeAxis.h"

static CGFloat const timeAxisHeight = 20;
static CGFloat const timeLineViewRate = .65;
@interface HACStockTimeLine () <UIScrollViewDelegate>
@property (nonatomic, strong) HACTimeLineView *timeLineView;
@property (nonatomic, strong) HACTecView *tecView;
@property (nonatomic, strong) HACStockTimeAxis *timeAxisView;

@end

@implementation HACStockTimeLine {
}

- (void)setUp {
    [super setUp];
    
    self.contentView.bounces = NO;
    
    //分时图
    _timeLineView = [[HACTimeLineView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:_timeLineView];
    
    //时间轴
    _timeAxisView = [[HACStockTimeAxis alloc] initWithFrame:CGRectZero];
    _timeAxisView.type = HACStockTimeAxisTypeMline;
//    _timeAxisView.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:_timeAxisView];
    //指标 量
    _tecView = [[HACTecView alloc] initWithFrame:CGRectZero];
    [_tecView setViewType:HACTecViewTypeTimeLine andIndexType:HACTecViewIndexTypeVol];
    [self.contentView addSubview:_tecView];

//    _tecView.leftAxis = self.timeLineView.leftAxis;
//    _tecView.rightAxis = self.timeLineView.rightAxis;
}

- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    CGFloat h = frame.size.height - timeAxisHeight;
    _timeLineView.frame = CGRectMake(0, 0, frame.size.width, h*timeLineViewRate);
    _timeAxisView.frame = CGRectMake(0, CGRectGetMaxY(_timeLineView.frame), frame.size.width, timeAxisHeight);
    _tecView.frame = CGRectMake(0, CGRectGetMaxY(_timeLineView.frame)+timeAxisHeight, self.frame.size.width, h*(1-timeLineViewRate));
}

- (HACAxisComponents *)leftAxis {
    return _timeLineView.leftAxis;
}

- (HACAxisComponents *)rightAxis {
    return _timeLineView.rightAxis;
}

- (void)setData:(HACChartData *)data {
    [super setData:data];
    [self.timeLineView setData:data];
    HACChartDataSet *tmp = [data dataSetWithLabel:HACBasicDataLabel];
    HACBasicInfoEntry *basicInfoData = tmp?(HACBasicInfoEntry*)tmp.values.firstObject:nil;
    [self.timeAxisView refreshUIWithTimeArray:basicInfoData.timeArray];
    [self.tecView setData:data];
    //tecview 暂无animation需刷新显示
    [self.tecView refreshUI];
}

- (void)refreshWithData:(HACChartData *)data {
    [super refreshWithData:data];
    [self.timeLineView setData:data];
    HACChartDataSet *tmp = [data dataSetWithLabel:HACBasicDataLabel];
    HACBasicInfoEntry *basicInfoData = tmp?(HACBasicInfoEntry*)tmp.values.firstObject:nil;
    [self.timeAxisView refreshUIWithTimeArray:basicInfoData.timeArray];
    [self.tecView setData:data];
    [self refreshUI];
}

- (void)startAnimateWithDuration:(NSTimeInterval)duration {
    [self.timeLineView.animator animateXWithDuration:duration];
}

- (void)refreshWithIncrementData:(HACChartData *)data {
    [super refreshWithIncrementData:data];
    [self.timeLineView addIncrementData:data];
    [self.tecView addIncrementData:data];
    [self refreshUI];
}

- (void)refreshUI {
    [self.timeLineView refreshUI];
    [self.tecView refreshUI];
}

- (void)reset {
    self.data = nil;
    [self.timeLineView clean];
    [self.tecView clean];
}

- (void)setVisualCount:(NSInteger)visualCount {
    [self.timeLineView setVisualCount:visualCount];
    [self.tecView setVisualCount:visualCount];
}
#pragma mark - actions
- (void)reactForLongPressGesture:(UILongPressGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self];
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        //偏移，因gesture所在view不随scroll移动
        point = CGPointMake(point.x + self.contentView.contentOffset.x, point.y + self.contentView.contentOffset.y);
        self.focusIndex = [self.timeLineView indexAtChartForCoordinate:point];;
        self.focusPoint = [self.timeLineView pointAtChartForIndex:self.focusIndex];
    } else {
//        self.focusIndex = -1;
    }
}

- (void)reactForContentViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    self.timeLineView.frame = CGRectMake(offset.x, self.timeLineView.frame.origin.y, self.timeLineView.frame.size.width, self.timeLineView.frame.size.height);
    self.tecView.frame = CGRectMake(offset.x, self.tecView.frame.origin.y, self.tecView.frame.size.width, self.tecView.frame.size.height);
}
#pragma mark - provider
- (NSArray *)focusDataInRect:(CGRect)rect focusIndex:(NSInteger)focusIndex {
    if (self.focusItemsCount == 0 || focusIndex >= self.timeLineView.actualCount || focusIndex == -1)
        return nil;
    
    NSMutableArray *focusDrawingData = [NSMutableArray array];
    CGFloat x = 0.0, y = 0.0;
    for (NSInteger i = 0; i < self.focusItemsCount; i++) {
        HACFocusComponents *item = [[HACFocusComponents alloc] init];
        item.index = i;
        item.axisType = i == 0 ? DrawingAxisTypeY : DrawingAxisTypeX; //第一根是竖线，第二根横线
        item.text = [self stringForFocusItem:item focusIndex:focusIndex];
        
        CGSize textSize = [item.text sizeWithAttributes:@{NSFontAttributeName : item.textFont}];
        CGRect frame = CGRectMake(CGRectGetMinX(rect), CGRectGetMinY(rect), textSize.width , textSize.height);
        CGLine line = CGLineMake(0, 0, 0, 0);
        
        CGPoint focusPoint = [self.timeLineView pointAtChartForIndex:focusIndex];
        focusPoint = CGPointMake(focusPoint.x, focusPoint.y);
        
        if (item.axisType == DrawingAxisTypeX) {
            y = focusPoint.y;
            line = CGLineMake(CGRectGetMinX(rect) + textSize.width, y, CGRectGetMaxX(rect), y);
            frame.origin.y = MIN(CGRectGetMaxY(rect), MAX(CGRectGetMinY(rect), y - textSize.height / 2));
        } else {
            x = focusPoint.x;
            line = CGLineMake(x, CGRectGetMinY(rect), x, CGRectGetMaxY(rect));
            frame.origin.x = MIN(CGRectGetMaxX(rect) - textSize.width, MAX(CGRectGetMinX(rect), x - textSize.width / 2));
            frame.origin.y = CGRectGetMaxY(rect);
        }
        
        item.textFrame = frame;
        item.line = line;
        item.point = focusPoint;
        [focusDrawingData addObject:item];
    }
    return focusDrawingData;
}

- (NSString *)stringForFocusItem:(HACFocusComponents *)focusItem focusIndex:(NSInteger)focusIndex {
    NSString *focusString;
    HACTimeLineEntry *data = [self.timeLineView dataAtChartForIndex:focusIndex];
    if (focusItem.axisType == DrawingAxisTypeX) {
        focusString = @(data.newprice).stringValue;
    } else {
        focusString = @(data.date).stringValue;
    }
    return focusString;
}

@end
